package com.hk.housekeeping.service.impl;/*
 * @author  pyh
 * @date  2021/1/30 0030 下午 2:04
 * @version 1.0
 * @since JDK1.8_241
 */

import com.hk.housekeeping.entity.Advertise;
import com.hk.housekeeping.mapper.IAdMapper;
import com.hk.housekeeping.service.AdvertiseService;
import com.hk.housekeeping.utils.PageUtils;
import com.hk.housekeeping.utils.ResponseCode;
import com.hk.housekeeping.utils.ResponseEntity;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdvertiseServiceImpl implements AdvertiseService {
    @Resource
    private IAdMapper iAdMapper;

    @Override
    public ResponseEntity<Integer> insert(Advertise advertise) {
        ResponseEntity<Integer> entity = null;
        int insert = iAdMapper.insert(advertise);
        if (insert != 0){
            entity = ResponseEntity.success(insert);
        }else {
            entity = ResponseEntity.error(ResponseCode.ADVERTISE_ADD_ERROR);
        }
        return entity;
    }

    @Override
    public ResponseEntity<Integer> del(int id) {
        ResponseEntity<Integer> entity = null;
        int del = iAdMapper.del(id);
        if (del != 0){
            entity = ResponseEntity.success(del);
        }else {
            entity = ResponseEntity.error(ResponseCode.ADVERTISE_DEL_ERROR);
        }
        return entity;
    }

    @Override
    public Advertise list(int id) {
        return iAdMapper.list(id);
    }
    //修改 先删除在插入
    @Override
    public ResponseEntity<Integer> update(Advertise advertise) {
        ResponseEntity<Integer> entity = null;
        if (!StringUtils.isEmpty(advertise)){
            //先删除
            int count1 = iAdMapper.del(advertise.getId());
            //再插入
            Advertise newAd = new Advertise();
            newAd.setName(advertise.getName());
            newAd.setAssociateditems(advertise.getAssociateditems());
            newAd.setStartTime(advertise.getStartTime());
            newAd.setEndTime(advertise.getEndTime());
            //访问人数按照回显的人数重新插入回去，否则默认为0
            newAd.setVisitors(advertise.getVisitors());
            int count2 = iAdMapper.insert(newAd);
            if (count1 == 0){
                //打印一下是否能删除信息
                System.out.println(ResponseCode.ADVERTISE_DEL_ERROR.getMsg());
                entity = ResponseEntity.error(ResponseCode.ADVERTISE_UPDATE_ERROR);
            }else if (count2 == 0){
                System.out.println(ResponseCode.ADVERTISE_ADD_ERROR.getMsg());
                entity = ResponseEntity.error(ResponseCode.ADVERTISE_UPDATE_ERROR);
            }else {
                //修改成功返回1
                entity = ResponseEntity.success(1);
            }
        }else {
            entity = ResponseEntity.error(ResponseCode.ADVERTISE_UPDATE_ERROR);
        }
        return entity;
    }

    //根据广告名称查询
    @Override
    public ResponseEntity<PageUtils<List<Advertise>>> selectByName(String name,int currentPage, int pageSize) {
        ResponseEntity<PageUtils<List<Advertise>>> entity = null;

        try {
            List<Advertise> adName = iAdMapper.getAdName(name, (currentPage - 1) * pageSize, pageSize);
            PageUtils<List<Advertise>> pageUtils = new PageUtils<>();
            pageUtils.setCurrentPage(currentPage);
            pageUtils.setTotalCount(iAdMapper.selectTotalCountByName(name));
            pageUtils.setData(adName);

            entity = ResponseEntity.success(pageUtils);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.ADVERTISE_BY_NAME_ERROR);
        }

        return entity;
    }

    //根据状态值查询
    @Override
    public ResponseEntity<PageUtils<List<Advertise>>> selectByStatus(int status, int currentPage, int pageSize) {
        ResponseEntity<PageUtils<List<Advertise>>> entity = null;
        try {
            List<Advertise> adStatus = iAdMapper.getAdStatus(status, (currentPage - 1) * pageSize, pageSize);
            PageUtils<List<Advertise>> pageUtils = new PageUtils<>();
            pageUtils.setCurrentPage(currentPage);
            pageUtils.setTotalCount(iAdMapper.selectTotalCountByStatus(status));
            pageUtils.setData(adStatus);

            entity = ResponseEntity.success(pageUtils);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.ADVERTISE_BY_NAME_ERROR);
        }
        return entity;
    }

    @Override
    public ResponseEntity<PageUtils<List<Advertise>>> selectByDouble(String name, int status, int currentPage, int pageSize) {
        ResponseEntity<PageUtils<List<Advertise>>> entity = null;
        try {
            List<Advertise> advertises = iAdMapper.selectAdByDouble(name, status, (currentPage - 1) * pageSize, pageSize);
            PageUtils<List<Advertise>> pageUtils = new PageUtils<>();
            pageUtils.setCurrentPage(currentPage);
            pageUtils.setTotalCount(iAdMapper.selectTotalCountByDouble(name, status));
            pageUtils.setData(advertises);

            entity = ResponseEntity.success(pageUtils);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.ADVERTISE_LIST_ERROR);
        }

        return entity;
    }

    //分页查询
    @Override
    public ResponseEntity<PageUtils<List<Advertise>>> pageSelect(int currentPage, int pageSize) {
        List<Advertise> staffs = iAdMapper.pageSelect((currentPage - 1) * pageSize, pageSize);
        PageUtils<List<Advertise>> pageList = new PageUtils<>();
        //改变当前页（设置为请求参数的页数）
        pageList.setCurrentPage(currentPage);
        //设置返回总页数
        pageList.setTotalCount(getTotalCount().getData());
        //设置返回内容
        pageList.setData(staffs);

        ResponseEntity<PageUtils<List<Advertise>>> entity = ResponseEntity.success(pageList);
        return entity;
    }
    //查询广告表的总条数
    @Override
    public ResponseEntity<Integer> getTotalCount() {
        int totalCount = iAdMapper.getTotalCount();
        ResponseEntity<Integer> entity = ResponseEntity.success(totalCount);
        return entity;
    }

}
